package cgb2202.recruitment01.controller;

import cgb2202.recruitment01.entity.Emp;
import cgb2202.recruitment01.entity.EmpInfo;
import cgb2202.recruitment01.mapper.EmpInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class EmpInfoController {
    @Autowired(required = false)
    EmpInfoMapper mapper;
    @RequestMapping("/empInfo/select")
    public EmpInfo select(HttpSession session){
        Emp emp=(Emp) session.getAttribute("currentUser");
        System.out.println(mapper.getEmpInfoByEmail(emp.getEmail()));
        return mapper.getEmpInfoByEmail(emp.getEmail());
    }

    @RequestMapping("/empInfo/insert")
    public void insertEmpInfo(@RequestBody EmpInfo empInfo, HttpSession session){
        Emp emp=(Emp) session.getAttribute("currentUser");
        int count=mapper.selectByUid(emp.getId());
        int uid=emp.getId();
        System.out.println(emp);
        System.out.println(emp.getId());
        System.out.println(empInfo);
        if (count == 0){
            mapper.insertEmpInfo(uid,empInfo);
        }else{
            mapper.updateEmpInfo(uid,empInfo);
        }
        System.out.println(emp.getEmail());
        mapper.getEmpInfoByEmail(emp.getEmail());
    }

}
